package com.example.service;

import com.example.dao.GoumaiordersInfoDao;
import com.example.dao.SigninusersInfoDao;
import com.example.entity.*;
import com.example.entity.GoumaiordersInfo;
import com.example.entity.SigninusersInfo;
import com.example.vo.GoumaiordersInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class GoumaiordersInfoService {

    @Resource
    private GoumaiordersInfoDao goumaiordersInfoDao;
	@Resource
    private SigninusersInfoService signinusersInfoService;
    
	
	@Resource
    private SigninusersInfoDao signinusersInfoDao;

    public GoumaiordersInfo add(GoumaiordersInfo goumaiordersInfo) {
        //shangxchxuantupxian
        // 唯一校验

      // goumaiordersInfo.setAccount((double) 0);
	  SigninusersInfo signinusersInfo = signinusersInfoDao.selectByUsername(goumaiordersInfo.getUsername());
        signinusersInfo.setYue(String.valueOf(Integer.valueOf(signinusersInfo.getYue())-Integer.valueOf(goumaiordersInfo.getGoumaiamount())));
        signinusersInfoDao.updateByPrimaryKeySelective(signinusersInfo);
        goumaiordersInfoDao.insertSelective(goumaiordersInfo);
        return goumaiordersInfo;
    }

	public List<Map<String,String>> findByDiqu() {
        return goumaiordersInfoDao.findByDiqu();
    }

    public void delete(Long id) {
        goumaiordersInfoDao.deleteByPrimaryKey(id);
    }

    public void update(GoumaiordersInfo goumaiordersInfo) {
        //shangxchxuantupxian
		//youdianzan
        goumaiordersInfoDao.updateByPrimaryKeySelective(goumaiordersInfo);
    }

    public GoumaiordersInfo findById(Long id) {
        return goumaiordersInfoDao.selectByPrimaryKey(id);
    }
	
    public List<GoumaiordersInfoVo> findAll() {
        return goumaiordersInfoDao.findByGoodsnumbers("all");
    }

    public PageInfo<GoumaiordersInfoVo> findPage(String goodsnumbers, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<GoumaiordersInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        if (user.getLevel().equals("管理员")) {all = goumaiordersInfoDao.findByGoodsnumbers(goodsnumbers);}
		if (user.getLevel().equals("注册用户")) {all = goumaiordersInfoDao.findByGoodsnumberslist2(goodsnumbers,user.getUsername()); }
        
        
        return PageInfo.of(all);
    }
	
	public PageInfo<GoumaiordersInfoVo> findPageqt(String goodsnumbers, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<GoumaiordersInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        all = goumaiordersInfoDao.findByGoodsnumbers(goodsnumbers);
		if (user.getLevel().equals("注册用户")) {all = goumaiordersInfoDao.findByGoodsnumberslist2(goodsnumbers,user.getUsername()); }
        
	
	
        
        return PageInfo.of(all);
    }

   // public GoumaiordersInfoVo findByUserName(String name) {
//        return goumaiordersInfoDao.findByUsername(name);
//    }

	//yoxulogindenxglu
	
    public void changeStatus(Long id) {
        GoumaiordersInfo goumaiordersInfo = goumaiordersInfoDao.selectByPrimaryKey(id);
        if(goumaiordersInfo.getStatus().equals("是")){
            goumaiordersInfo.setStatus("否");
            goumaiordersInfoDao.updateByPrimaryKey(goumaiordersInfo);
        }else if(goumaiordersInfo.getStatus().equals("否")){
            goumaiordersInfo.setStatus("是");
            goumaiordersInfoDao.updateByPrimaryKey(goumaiordersInfo);
        }
    }
	//ddaizdhifu
	//youtixing
	
	
	
	public List<GoumaiordersInfo> getBysigninusersUsername(String username) {
    SigninusersInfo signinusersInfo = signinusersInfoService.findByUsername(username);
    return goumaiordersInfoDao.findByusername(signinusersInfo);
    }
    
	
	//xiugaimimazys
	
}
